package com.kungfuhacking.wristbandpro.login.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.InputType;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.kungfuhacking.wristbandpro.R;
import com.kungfuhacking.wristbandpro.base.view.BaseActivity;
import com.kungfuhacking.wristbandpro.custom.InputView;
import com.kungfuhacking.wristbandpro.custom.TitleBarView;
import com.kungfuhacking.wristbandpro.login.presenter.IRegisterPresenter;
import com.kungfuhacking.wristbandpro.login.view.IRegisterView;


/**
 * 注册页面
 * Created by Aaron on 17/3/28.
 */

public class RegisterActivity extends BaseActivity implements View.OnClickListener, IRegisterView {

    private TextView tvgetverifycode;
    private EditText etverifycode;
    private ImageView ivagree;
    private TextView agreedetail;
    private TextView tv_next;
    private InputView iv_account;
    private InputView iv_nickname;
    private TitleBarView tbv;

    private IRegisterPresenter iRegisterPresenter = new IRegisterPresenter(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_register);

        initWidget();
        initListener();

    }

    private void initWidget() {

        this.tv_next = (TextView) findViewById(R.id.tv_next);
        this.agreedetail = (TextView) findViewById(R.id.agree_detail);
        this.ivagree = (ImageView) findViewById(R.id.iv_agree);
        iv_account = (InputView) findViewById(R.id.iv_account);
        iv_nickname = (InputView) findViewById(R.id.iv_nickname);
        this.etverifycode = (EditText) findViewById(R.id.et_verify_code);
        this.tvgetverifycode = (TextView) findViewById(R.id.tv_get_verify_code);
        this.tbv = (TitleBarView) findViewById(R.id.tbv);

        TextView tvCustomer = (TextView) findViewById(R.id.tvCustomer);
        tvCustomer.setOnClickListener(this);

        ivagree.setSelected(true);

        //设置部分字体颜色
        setTextColor();

        iv_account.setInputType(InputType.TYPE_CLASS_PHONE);

    }

    private void setTextColor() {
        SpannableStringBuilder builder = new SpannableStringBuilder(agreedetail.getText().toString());
        ForegroundColorSpan redSpan = new ForegroundColorSpan(getResources().getColor(R.color.bbd));
        builder.setSpan(redSpan, 3, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        agreedetail.setText(builder);
    }

    private void initListener() {

        tvgetverifycode.setOnClickListener(this);
        agreedetail.setOnClickListener(this);
        tv_next.setOnClickListener(this);
        ivagree.setOnClickListener(this);

        tbv.setOnLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.tv_get_verify_code://获取验证码
                iRegisterPresenter.getSmsCode();
                break;

            case R.id.iv_agree:
                ivagree.setSelected(!ivagree.isSelected());
                break;

            case R.id.agree_detail://使用条款
                nextActivity(ClauseActiviry.class);
                break;

            case R.id.tv_next://下一步
                iRegisterPresenter.submitConfirm();//提交验证,进入下一步
                break;

            case R.id.tvCustomer://客服电话

                callCusromerService();
                break;

        }
    }

    private void callCusromerService() {
        new AlertDialog.Builder(this).setMessage("0411-39041379").setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).setPositiveButton("呼叫", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                dialog.dismiss();
                //拨打电话
                Intent intent = new Intent("android.intent.action.CALL", Uri.parse("tel:0411-39041379"));
                nextActivityWithIntent(intent);

            }
        }).show();
    }

    @Override
    public String getNum() {
        return iv_account.getText();
    }

    @Override
    public String getCode() {
        return etverifycode.getText().toString();
    }

    @Override
    public String getNickname() {
        return iv_nickname.getText();
    }

    @Override
    public boolean isAgree() {
        return ivagree.isSelected();
    }

    @Override
    public void setUnclickable() {
        tvgetverifycode.setClickable(false);
        tvgetverifycode.setEnabled(false);
    }

    @Override
    public void setClickable() {
        tvgetverifycode.setClickable(true);
        tvgetverifycode.setEnabled(true);
    }

    @Override
    public void setCountTime(String time) {
        tvgetverifycode.setText(time);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        iRegisterPresenter.clearRequest();
    }

}
